package com.xsw.common.config;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * 小程序配置文件
 *
 * @describe:
 * @author: jiazl /
 * @version: v1.0
 */
@Component
@ConfigurationProperties(prefix = "miniapp.configs")
public class MiniConfig {

    private String appid;

    private String secret;


    @Bean("wxService")
    public WxMaService wxService() {
        if (appid == null || secret == null) {
            throw new WxRuntimeException("required wechat param not found");
        }
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(appid);
        config.setSecret(secret);
//        config.setToken(properties.getToken());
//        config.setAesKey(properties.getAesKey());
//        config.setMsgDataFormat(properties.getMsgDataFormat());
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(config);
        return service;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }
}
